package com.wskj.web.controller.system;

import java.util.List;
import java.util.Arrays;

import com.wskj.system.bo.SysBannerAddBo;
import com.wskj.system.bo.SysBannerEditBo;
import com.wskj.system.bo.SysBannerQueryBo;
import com.wskj.system.service.ISysBannerService;
import com.wskj.system.vo.web.SysBannerVo;
import lombok.AllArgsConstructor;

import javax.validation.constraints.*;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.multipart.MultipartFile;
import com.wskj.common.annotation.RepeatSubmit;
import com.wskj.common.annotation.Log;
import com.wskj.common.core.controller.BaseController;
import com.wskj.common.core.domain.AjaxResult;
import com.wskj.common.enums.BusinessType;
import com.wskj.common.exception.CustomException;
import com.wskj.common.utils.poi.ExcelUtil;
import com.wskj.common.core.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiImplicitParam;

/**
 * 轮播图Controller
 *
 * @author wsdz
 * @date 2023-03-02
 */
@Api(value = "轮播图控制器", tags = {"轮播图管理"})
@AllArgsConstructor
@RestController
@RequestMapping("/system/banner")
public class SysBannerController extends BaseController {

    private final ISysBannerService iSysBannerService;

    /**
     * 查询轮播图列表
     */
    @ApiOperation("查询轮播图列表")
    @PreAuthorize("@ss.hasPermi('system:banner:list')")
    @GetMapping("/list")
    public TableDataInfo<SysBannerVo> list(@Validated SysBannerQueryBo bo) {
        return iSysBannerService.queryPageList(bo);
    }

    /**
     * 导出轮播图列表
     */
    @ApiOperation("导出轮播图列表")
    @PreAuthorize("@ss.hasPermi('system:banner:export')")
    @Log(title = "轮播图", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult<Void> export(@Validated SysBannerQueryBo bo) {
        List<SysBannerVo> list = iSysBannerService.queryList(bo);
        ExcelUtil<SysBannerVo> util = new ExcelUtil<>(SysBannerVo.class);
        return util.exportExcel(list, "轮播图");
    }

    /**
     * 获取轮播图详细信息
     */
    @ApiOperation("获取轮播图详细信息")
    @PreAuthorize("@ss.hasPermi('system:banner:query')")
    @GetMapping("/{bannerId}")
    public AjaxResult<SysBannerVo> getInfo(@NotNull(message = "主键不能为空")
                                                  @PathVariable("bannerId") Long bannerId) {
        return AjaxResult.success(iSysBannerService.queryById(bannerId));
    }

    /**
     * 新增轮播图
     */
    @ApiOperation("新增轮播图")
    @PreAuthorize("@ss.hasPermi('system:banner:add')")
    @Log(title = "轮播图", businessType = BusinessType.INSERT)
    @RepeatSubmit
    @PostMapping()
    public AjaxResult<Void> add(@Validated @RequestBody SysBannerAddBo bo) {
        return toAjax(iSysBannerService.insertByAddBo(bo) ? 1 : 0);
    }

    /**
     * 修改轮播图
     */
    @ApiOperation("修改轮播图")
    @PreAuthorize("@ss.hasPermi('system:banner:edit')")
    @Log(title = "轮播图", businessType = BusinessType.UPDATE)
    @RepeatSubmit
    @PutMapping()
    public AjaxResult<Void> edit(@Validated @RequestBody SysBannerEditBo bo) {
        return toAjax(iSysBannerService.updateByEditBo(bo) ? 1 : 0);
    }

    /**
     * 删除轮播图
     */
    @ApiOperation("删除轮播图")
    @PreAuthorize("@ss.hasPermi('system:banner:remove')")
    @Log(title = "轮播图" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{bannerIds}")
    public AjaxResult<Void> remove(@NotEmpty(message = "主键不能为空")
                                       @PathVariable Long[] bannerIds) {
        return toAjax(iSysBannerService.deleteWithValidByIds(Arrays.asList(bannerIds), true) ? 1 : 0);
    }

    /**
     * 导入轮播图数据
     */
    @ApiOperation("导入轮播图数据")
    @ApiImplicitParam(name = "file", value = "文件", required = true, dataType = "file")
    @PreAuthorize("@ss.hasPermi('system:banner:import')")
    @Log(title = "轮播图" , businessType = BusinessType.IMPORT)
    @PostMapping("/import")
    public AjaxResult<Void> importData(MultipartFile file) {

        ExcelUtil<SysBannerVo> util = new ExcelUtil<>(SysBannerVo.class);
        List<SysBannerVo> sysBannerList;
        try {
            sysBannerList = util.importExcel(file.getInputStream());
        } catch (Exception e) {
            e.printStackTrace();
            throw new CustomException("文件解析异常！message：" + e.getMessage());
        }
        String message = iSysBannerService.importSysBanner(sysBannerList);
        return AjaxResult.success(message);

    }

    /**
     * 下载模板
     *
     * @return AjaxResult 模板文件
     */
    @ApiOperation("下载模板")
    @GetMapping("/importTemplate")
    public AjaxResult importTemplate() {
        ExcelUtil<SysBannerVo> util = new ExcelUtil<>(SysBannerVo.class);
        return util.importTemplateExcel("轮播图数据");
    }
}
